#ifndef MAINDIALOG_H
#define MAINDIALOG_H

#include <QDialog>
#include <QSharedPointer>
#include <QCamera>
#include <QMediaRecorder>
#include <QTimer>

const QString RECORD = QObject::tr("Record");
const QString STOP_RECORDING = QObject::tr("Stop recording");
const QString CHOOSE_DIRECTORY = QObject::tr("Choose directory");

namespace Ui {
class MainDialog;
}

class MainDialog : public QDialog
{
    Q_OBJECT

public:
    enum RunningType{CameraOff, CameraOn, Image, Video, AutoSnapping, Motion};
    static const int mainCameraIndex = 0;
    static const int cameraSettingsIndex = 1;
    explicit MainDialog(QWidget *parent = 0);
    ~MainDialog();
    void setCamera(const QByteArray &cameraDevice);
    void eventuallyStopCamera(const QByteArray &nextCameraDevice);
    void stopCamera();
    bool inCameraTab();
    bool inCameraSettingsTab();
    void changeModeAndSetCameraOffMode();
    void setCameraOffMode();
    void setCameraOnMode();
    void setCameraImageMode();
    void setCameraVideoMode();
    void setCameraAutoSnappingMode();
    void setCameraMotionMode();
    bool isRecording();
    void sendEmail(QString title, QString content, QUrl imageFilePath);

public slots:
    void setCamera(int currentCameraIndex);
    void setRightViewFinder();
    void onTabIndexChanged(int tabIndex);
    void onSelectionTableCellChanged(int x, int y);
    void showDirectory();
    void record();
    void recordImage();
    void recordVideo();
    void recordVideoStart();
    void recordVideoStop();
    void recordAutoSnapping();
    void recordMotion();
    void authenticationFailed(const QByteArray & msg);
    void connectionFailed(const QByteArray & msg);
    void mailFailed(int mailID, int errorCode, const QByteArray & msg);
    //void	setFullScreen(bool fullScreen);
    //Camera
    void setBrightness(int brightness);
    void setContrast(int contrast);
    void setHue(int hue);
    void setSaturation(int saturation);
    //Settings
    void chooseImagesDirectory();
    void chooseVideosDirectory();
    void chooseAutoSnappingDirectory();
    void chooseMotionDirectory();
    void setImagesDirPath(QString imagesDirPath);
    void setVideosDirPath(QString videosDirPath);
    void setAutoSnappingDirPath(QString autoSnappingDirPath);
    void setMotionDirPath(QString motionDirPath);
    void setHidePasswordSender(bool hide);

private slots:
    void _recordAndSendImage();
    void _recordAndSendImageMotionEventually();

private:
    void _connectSlots();
    void _connectMainTabSlots();
    void _connectCameraSettingsSlots();
    void _connectSettingsSlots();
    void _loadSettings();
    void _loadAvailableWebcams();
    void _initCameraFromPreviousSession();
    void _enterEmailInformation();
    RunningType currentMode;
    Ui::MainDialog *ui;
    QSharedPointer<QCamera> camera;
    QSharedPointer<QMediaRecorder> mediaRecorder;
    QString currentCameraDevice;
    QTimer *timerAutoSnapping;
    QTimer *timerMotion;
    int lastTabIndex;
    void _updateAfterCameraChanged(const QByteArray &nextCameraDevice);
    void _updateFromMainTabToSettingsCamera();
    void _updateFromSettingsCameraToMainTab();
    bool enableCameraForOtherMode();
    void _setMode(RunningType mode);
};

#endif // MAINDIALOG_H
